import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  DeptApi,
  DeptPage,
  DeptReq,
  DeptCreateReq,
  DeptUpdateReq,
  DeptEnableKeyList,
  DeptKeyList,
  DeptMoveKey,
  DeptKey,
  DeptModel,
  DeptExportReq,
  DeptImportReq,
  DeptEnableKey,
  DeptList,
} from "./types";
import qs from "qs";

class DeptApiImpl implements DeptApi {
  getKey(model: DeptModel): DeptKey {
    return {
      deptId: model.deptId,
    } as DeptKey;
  }

  /**
   * 获取分页数据
   *
   * @param queryParams 查询参数，可选
   * @returns 返回请求结果
   */
  findPage(query: DeptReq): AxiosPromise<DeptPage> {
    query.tenant = useUserStore().user.tenant;
    if (query) {
      query.hasStatus = query.status != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/rbac/dept/list",
      method: "get",
      params: query,
    });
  }
  /**
   * 查询部门列表
   *
   * @param query 查询参数，符合DeptReq接口规范
   * @returns 返回AxiosPromise，包含DeptList类型的列表数据
   */
  findList(query?: DeptReq): AxiosPromise<DeptList> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/dept/list?tenant=" + tenant,
      method: "get",
      params: query,
    });
  }
  // 查询组织机构
  findById(query: DeptKey): AxiosPromise<DeptModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/dept/id",
      method: "get",
      params: query,
    });
  }

  /**
   * 添加部门
   *
   * @param data 部门信息
   * @returns 返回请求结果
   */
  create(data: DeptCreateReq): AxiosPromise<DeptModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/dept",
      method: "post",
      data: data,
    });
  }
  /**
   * 编辑部门信息
   *
   * @param data 编辑后的部门数据
   * @returns 返回请求结果
   */
  update(data: DeptUpdateReq): AxiosPromise<DeptModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/dept",
      method: "patch",
      data: data,
    });
  }

  /**
   * 删除部门
   *
   * @param deptId 部门ID
   * @returns 返回请求结果
   */
  delete(query: DeptKey): AxiosPromise<DeptModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/dept/id",
      method: "delete",
      params: query,
    });
  }

  /**
   * 批量删除部门
   *
   * @param ids 部门ID数组
   * @returns 返回请求结果
   */
  batchDelete(keys: DeptKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.deptId);
    const data: DeptKeyList = {
      tenant: useUserStore().user.tenant,
      deptId: ids,
    } as DeptKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/dept/batch",
      method: "put",
      data: data,
    });
  }
  /**
   * 向上/下移动部门
   *
   * @param roleId 部门ID
   * @returns 返回Promise对象，表示异步操作的结果
   */
  move(key: DeptKey, up: boolean): AxiosPromise<boolean> {
    const data: DeptMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/dept/move",
      method: "put",
      data: data,
    });
  }
  /**
   * 获取部门树形列表
   *
   * @param deptId 部门ID，可选
   * @returns 返回部门树形列表
   */
  treelistDept(deptId?: string): AxiosPromise {
    const query = {
      tenant: useUserStore().user.tenant,
      excludeId: deptId,
    };
    return request({
      url: "/v1/rbac/dept/treelist",
      method: "get",
      params: query,
    });
  }
  /**
   * 下载部门模板
   *
   * @returns 返回请求的 Promise 对象
   */

  downloadTemplate(): AxiosPromise {
    return request({
      url: "/v1/rbac/dept/template.xlsx",
      method: "get",
      responseType: "blob",
      params: {
        tenant: useUserStore().user.tenant,
      },
    });
  }

  /**
   * 导入资源
   *
   * @param data 资源数据
   * @returns 返回请求结果
   */

  importExcel(data: DeptImportReq): AxiosPromise<string> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/dept/import",
      method: "patch",
      data: data,
    });
  }

  /**
   * 导出资源到Excel文件
   *
   * @param queryParams 查询参数
   * @returns 返回一个Promise对象，用于处理异步操作
   */
  exportExcel(queryParams: DeptExportReq): AxiosPromise {
    clearEmpty(queryParams);
    const query = qs.stringify(queryParams, {
      arrayFormat: "brackets",
      allowDots: true,
    });
    return request({
      url:
        "/v1/rbac/dept/" + useUserStore().user.tenant + "/depts.xlsx?" + query,
      method: "get",
      responseType: "blob",
    });
  }

  /**
   * 启用禁用用户API
   *
   * @param userId 用户ID
   * @returns 返回请求结果
   */
  enable(key: DeptKey, enable: boolean): AxiosPromise<boolean> {
    const data: DeptEnableKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enable = enable;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/dept/enable",
      method: "put",
      data: data,
    });
  }
  /**
   * 部门锁定接口
   *
   * @param data 请求参数
   * @returns 返回请求结果
   */
  batchEnable(key: DeptKeyList, enable: boolean): AxiosPromise {
    const data: DeptEnableKeyList = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enable = enable;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/dept/lock",
      method: "put",
      data: data,
    });
  }
}
const deptApi = new DeptApiImpl();
export { deptApi };
